import Vue from "vue"
import VueRouter from "vue-router"
import store from "@/store"
import { home } from "./module/home"

/**
 *
 * meta除了原生参数外可配置的参数:
 * meta: {
 *  title: { String|Number|Function }
 *         显示在侧边栏、面包屑和标签栏的文字
 *         使用'{{ 多语言字段 }}'形式结合多语言使用，例子看多语言的路由配置;
 *         可以传入一个回调函数，参数是当前路由对象，例子看动态路由和带参路由
 *  hideInBread: (false) 设为true后此级路由将不会出现在面包屑中，示例看QQ群路由配置
 *  hideInMenu: (false) 设为true后在左侧菜单不会显示该页面选项,
 *  privilegeExtend:{String} 同一功能模块下子页面的功能点权限继承菜单模块创建的路由权限  by lihaifan&lipeng
 *  noKeepAlive: (false) 设为true后页面在切换标签后不会缓存，如果需要缓存，无需设置这个字段，而且需要设置页面组件name属性和路由配置的name一致
 *  noValidatePrivilege: (true) 表示此路由不需要验证权限
 *  topMenu:(true)，表示为顶级菜单
 * }
 */
Vue.use(VueRouter)

/*
对VueRouter原型链上的push、replace方法进行重写
为了解决某些情况下控制台报 ‘Uncaught (in promise) undefined’的问题
*/
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject)
    return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch((err) => err)
}

const routes = [
  {
    path: "/login",
    name: "Login",
    component: () => import("@/views/login/index.vue"),
    meta: {
      hideInMenu: true,
      title: "登录",
      noValidatePrivilege: true,
    },
  },
  {
    path: "/",
    redirect: "/index",
    name: "layout",
    component: () => import("@/views/layout/index.vue"),
    meta: {},
    children: [...home],
  },
]

const router = new VueRouter({
  base: process.env.BASE_URL,
  mode: "history",
  routes,
})

router.beforeEach((to, from, next) => {
  // 后端返回路由导入路由列表
  if (!to.matched.length) {
    let routeList = store.state.permission.routeList
    routeList.map((item) => {
      item.name = item.call_url.split("/")[item.call_url.split("/").length - 1]
    })
    routeList = routeList.filter((item) => {
      return item.call_url !== ""
    })
    if (routeList.length > 0) {
      routeList = routeList.find((item) => {
        return item.call_url === to.path
      })
      let nowRoute = {
        path: routeList.call_url,
        name: routeList.call_url.split("/")[
          routeList.call_url.split("/").length - 1
        ],
        component: () => import(`@/views${routeList.path_url}.vue`),
        meta: {
          title: routeList.menu_name,
          power: routeList.api_authority,
        },
      }
      router.addRoute("layout", nowRoute)
      next({
        path: to.fullPath,
      })
    }
  } else {
    next()
  }
  // 当前路由标记,访问过路由记录
  if (to.name && to.name !== "Login") {
    // 记录访问过路由缓存
    let currentRoute = {
      path: to.path,
      name: to.meta.title,
    }
    let recordArr = store.state.permission.recordRoute
    let isSave = recordArr.some((item) => {
      return item.path === currentRoute.path
    })
    if (!isSave) {
      recordArr.push(currentRoute)
    }
    store.commit("setRecordRoute", recordArr)
    // 当前路由查询路由树找父节点缓存当前位置
    let nowMenu = store.state.permission.routeList.find((item) => {
      return to.meta.title === item.menu_name
    })
    let arr = []
    arr.unshift(nowMenu.menu_name)
    while (nowMenu.parent_id !== 0) {
      nowMenu = store.state.permission.routeList.find((item) => {
        return item.menu_id === nowMenu.parent_id
      })
      arr.unshift(nowMenu.menu_name)
    }
    store.commit("setCurrentPosition", arr)
  }
})

export default router
